﻿using B.S.Base.Write.API.Application.Command;
using MediatR;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;
using System.Linq;

namespace B.S.Base.Write.API.Application.CommandHandLer
{
    public class UpdateStatuCommandHandLer : IRequestHandler<UpdateStatuCommand, APIResult<int>>
    {
        private readonly IBaseRepostiy<ApproveModel> _repostiy;
        private readonly IBaseRepostiy<AccountModel> _accountModel;
        private readonly IBaseRepostiy<UserRoleModel> _userRoleModel;
        private readonly IBaseRepostiy<RoleModel> _roleModel;

        public UpdateStatuCommandHandLer(IBaseRepostiy<ApproveModel> repostiy, IBaseRepostiy<AccountModel> accountModel, IBaseRepostiy<UserRoleModel> userRoleModel, IBaseRepostiy<RoleModel> roleModel)
        {
            _repostiy = repostiy;
            _accountModel = accountModel;
            _userRoleModel = userRoleModel;
            _roleModel = roleModel;
        }

        public async Task<APIResult<int>> Handle(UpdateStatuCommand request, CancellationToken cancellationToken)
        {


            var list = _repostiy.GetAll().Where(x => x.Id == request.Id).FirstOrDefault();
            if (request.Statu == "不通过")
            {
                if (string.IsNullOrWhiteSpace(request.Examine))
                {
                    return new APIResult<int>
                    {
                        Code = ResultCode.Fail,
                        Data = 0,
                        Message = "请填写不通过原因" 
                    };
                }
                list.Statu = "不通过";
                list.Examine = request.Examine;
            }
            else if (request.Statu == "已审批")
            {
                list.Statu = "已审批";
                list.Examine = null;
            }
            await _repostiy.Update(list);
            return new APIResult<int>
            {
                Code = ResultCode.Ok,
                Data = 1,
                Message = "审批成功"
            };
        }
    }
}
